package com.xxwy.ddu.common.autowired.baserun;

import com.xxwy.ddu.common.autowired.entity.RabbitmqU;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @author xxwy
 * on 2018/8/9 0009
 * 必须要执行的类：注册路由，队列，绑定
 */
@Component
public class RabbitmqRun implements ApplicationRunner {

    @Autowired
    AmqpAdmin amqpAdmin;

    public void email(){
        amqpAdmin.declareExchange(new DirectExchange(RabbitmqU.MAIL_DIRECT));
        amqpAdmin.declareQueue(new Queue(RabbitmqU.MAIL_ATTACHMENT_QUEUE));
        amqpAdmin.declareQueue(new Queue(RabbitmqU.MAIL_SIMPLE_QUEUE));
        amqpAdmin.declareQueue(new Queue(RabbitmqU.MAIL_HTML_QUEUE));

        amqpAdmin.declareBinding(new Binding(RabbitmqU.MAIL_SIMPLE_QUEUE,
                Binding.DestinationType.QUEUE,RabbitmqU.MAIL_DIRECT,RabbitmqU.MAIL_SIMPLE_ROUTINGKEY,null));

        amqpAdmin.declareBinding(new Binding(RabbitmqU.MAIL_HTML_QUEUE,
                Binding.DestinationType.QUEUE,RabbitmqU.MAIL_DIRECT,RabbitmqU.MAIL_HTML_ROUTINGKEY,null));

        amqpAdmin.declareBinding(new Binding(RabbitmqU.MAIL_ATTACHMENT_QUEUE,
                Binding.DestinationType.QUEUE,RabbitmqU.MAIL_DIRECT,RabbitmqU.MAIL_ATTACHMENT_ROUTINGKEY,null));
    }

    @Override
    public void run(ApplicationArguments args) throws Exception {
        email();
    }
}
